//
// Created by hsj on 2022/1/5.
//
#include <iostream>
#include "t_recur.h"

//测试递归
void test_recur_all(){
    using namespace std;
    int sum = 0;
    test_recur(5,&sum);

    cout<<endl<<"OK,all recur complete,sum is:"<<sum<<endl;


    //打印标尺
    int len = 66;
    int divs = 6;
    char ch_arr[len];
    int i;
    for (i = 1; i < len - 2; ++i) {//填充中间作用域，除了两边之外
        ch_arr[i] = ' ';
    }
    ch_arr[len - 1] = '\0';//最后一个填充结束标识
    int max = len - 2;
    int min = 0;

    //两边是‘|’
    ch_arr[min] = ch_arr[max] = '|';
    cout<<ch_arr<<endl;

    for (i = 1; i < divs; ++i) {
        ruler(ch_arr,min,max,i);
        cout<<ch_arr<<endl;
        //重置,准备下一次循环
        for (int j = 1; j < len - 2; ++j) {
            ch_arr[i] = ' ';
        }
    }

}
void test_recur(int n,int *sum){
    using namespace std;

    cout<<"Counting down......"<<n<<endl;
    if (n > 0){
        *sum = *sum + n;
        test_recur(n - 1,sum);
    }
    cout<<n<<":down complete."<<endl;
}

//打印标尺
void ruler(char ch_arr[],int low,int hight,int level){

    if (0 == level){return;}
    int mid = (low + hight) / 2;
    ch_arr[mid] = '|';
    ruler(ch_arr,low,mid,level - 1);
    ruler(ch_arr,mid,hight,level - 1);
}